var queryData = {};
var nowSelectDept;
var nowSelectArear;
//记录点击
var deptNameValue;
var personNameValue;
var arearNameValue;
jQuery(function() {
	initQueryParam();
	jQuery("#queryBtn").on('click',function(){
		queryAllChart();
	})
	//还原按钮
	jQuery("#datehy").on('click',function(){
		var mydate = new Date();
		var begindate = mydate.getFullYear()+"-01-01"
		var enddate = "" + mydate.getFullYear() + "-";
		enddate += ((mydate.getMonth()+1)<10 ? '0'+(mydate.getMonth()+1) : (mydate.getMonth()+1)) + "-";
		enddate += (mydate.getDate()<10 ? '0'+mydate.getDate() : mydate.getDate());
		jQuery("#beginDate").val(begindate);
		$('#endDate').val(getDay(1));
		
		
		queryAllChart();
	})
	
	dept = echarts.init(document.getElementById('dept'));//部门分析
	arearThend = echarts.init(document.getElementById('arearThend'));//片区分析
	personChar = echarts.init(document.getElementById('personChar'));//人员分析
	myTypeChart = echarts.init(document.getElementById('myTypeChart'));//费用类型分析
	//部门分析
	dept.setOption({
        title: {
            text: '部门分析'
        },
        tooltip: {},
        series : [{
              name: '金额',
              type: 'pie',
              radius: '55%',
              data:[],
              itemStyle:{ 
                  normal:{ 
                        label:{ 
                          show: true, 
                          formatter: '{b} : {c} ({d}%)' 
                        }, 
                        labelLine :{show:true} 
                      } 
                  } 
        }]
    });
	
	//部门双击
	dept.on('dblclick',function(params){
    	openContractList(2,params.name);
    });
	//片区双击
	arearThend.on('dblclick',function(params){
    	openContractList(3,params.name);
    });
	//片区分析
	arearThend.setOption({
        title: {
            text: '片区分析'
        },
        tooltip: {},
        series : [{
              name: '数量',
              type: 'pie',
              radius: '55%',
              data:[],
              itemStyle:{ 
                  normal:{ 
                        label:{ 
                          show: true, 
                          formatter: '{b} : {c} ({d}%)' 
                        }, 
                        labelLine :{show:true} 
                      } 
                  } 
        }]
    });
	//部门分析单击
	dept.on('click', function (params) {
    	queryData.beginDate = jQuery('#beginDate').val();
    	queryData.endDate = jQuery('#endDate').val();
    	deptNameValue = params.name;
    	personNameValue = null;
    	arearNameValue = null;
    	nowSelectDept = params.name;
    	queryData.deptName = params.name;
    	var arearThendUrl= window.rootPath + '/ui/myMoneyInfo/getArearChart';//片区分析
    	jQuery.ajax({//片区分析
			url : encodeURI(arearThendUrl),
			type : 'get',
			data : queryData,
			dataType : 'json',
			contentType : 'application/json;charset=utf-8',
			success : function(data) {
				// 指定图表的配置项和数据
				arearThend.setOption({
			        series: [{
		              name: '金额',
		              type: 'pie',
		              radius: '55%',
		              data:data.data
			        }]
			    });
			}
		});
    	typeChart(params.name,null,null,4);
    });
	//片区双击
	personChar.on('dblclick',function(params){
    	openContractList(4,params.name);
    });
	//人员分析
	personChar.setOption({
        title: {
            text: '人员费用分析'
        },
        tooltip: {},
        series : [{
              name: '金额',
              type: 'pie',
              radius: '55%',
              data:[],
              itemStyle:{ 
                  normal:{ 
                        label:{ 
                          show: true, 
                          formatter: '{b} : {c} ({d}%)' 
                        }, 
                        labelLine :{show:true} 
                      } 
                  } 
        }]
    });
	//人员分析单击
	personChar.on('click',function(params){
		typeChart(nowSelectDept,nowSelectArear,params.name,6);
		personNameValue = params.name;
    });
	//片区分析单击
	arearThend.on('click', function (params) {
    	queryData.beginDate = jQuery('#beginDate').val();
    	queryData.endDate = jQuery('#endDate').val();
    	queryData.deptName = nowSelectDept;
    	nowSelectArear =  params.name;
    	personNameValue = null;
    	arearNameValue = params.name;
    	queryData.arear = params.name;
    	var arearThendUrl= window.rootPath + '/ui/myMoneyInfo/getPersonChart';//片区分析
    	jQuery.ajax({//片区分析
			url : encodeURI(arearThendUrl),
			type : 'get',
			data : queryData,
			dataType : 'json',
			contentType : 'application/json;charset=utf-8',
			success : function(data) {
				// 指定图表的配置项和数据
				personChar.setOption({
			        series: [{
		              name: '金额',
		              type: 'pie',
		              radius: '55%',
		              data:data.data
			        }]
			    });
			}
		});
    	typeChart(nowSelectDept,params.name,null,5)
    });
	//费用单击
	myTypeChart.on('click',function(params){
    	openContractList(5,params.name);
    });
	//费用类型分析
    myTypeChart.setOption({
        title: {
            text: '费用类型'
        },
        tooltip: {},
        legend: {
            data:['金额']
        },
        xAxis: {
            data: []
        },
        yAxis: {
        	 
        },
        itemStyle : {
        	normal: {
        		show: true, 
        		position: 'top',
        		color: function(params) {
                    var colorList = [
                       '#FF3333','#00c0ef'
                       ];
                    return colorList[params.dataIndex%2]
                },
        	}
        },
        series: [{
            name: '金额',
            type: 'bar',
            barWidth : 50,
            data: [],
            itemStyle: {  
                normal: {  
                    label: {  
                        show: true,//是否展示  
                        position: 'top',
                        textStyle: {  
                            fontWeight:'bolder',  
                            fontSize : '12',  
                            fontFamily : '微软雅黑',  
                        }  
                    }  
                }  
            }, 
        }],

    });
	//初始化加载一次非片区报表
    queryAllChart();
});
//初始化查询条件
function initQueryParam(){
	//$('#endDate').val(new Date().getFullYear()+"-"+((new Date().getMonth() + 1+"").length == 1 ? ("0"+(new Date().getMonth() + 1)) : new Date().getMonth() + 1) +"-"+((new Date().getDate()+"").length == 1 ? ("0"+new Date().getDate()) : new Date().getDate()));
	$('#beginDate').val(new Date().getFullYear()+"-01-01");
	$('#endDate').val(getDay(1));
}
function getDay(day){  
    var today = new Date();  
      
    var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;          

    today.setTime(targetday_milliseconds); //注意，这行是关键代码    
      
    var tYear = today.getFullYear();  
    var tMonth = today.getMonth();  
    var tDate = today.getDate();  
    tMonth = doHandleMonth(tMonth + 1);  
    tDate = doHandleMonth(tDate);  
    return tYear+"-"+tMonth+"-"+tDate;  
}  
function doHandleMonth(month){  
    var m = month;  
    if(month.toString().length == 1){  
       m = "0" + month;  
    }  
    return m;  
} 
//费用类型柱状图加载
function typeChart(deptname,arearname,personname,count){
	queryData.beginDate = jQuery('#beginDate').val();
	queryData.endDate = jQuery('#endDate').val();
	queryData.deptName = deptname;
	queryData.arear = arearname;
	queryData.person = personname;
	var personUrl = window.rootPath + '/ui/myMoneyInfo/getPersonNameChart?count='+count;
	jQuery.ajax({
		url : encodeURI(personUrl),
		type : 'get',
		data : queryData,
		dataType : 'json',
		contentType : 'application/json;charset=utf-8',
		success : function(data) {
			// 指定图表的配置项和数据
			myTypeChart.setOption({
		        xAxis: {
		            data: data.Axis
		        },
		        series: [{
		            // 根据名字对应到相应的系列
		            name: '销售额(元)',
		            barWidth : 50,
		            data: data.series
		        }]
		    });
		}
	});
}
function queryAllChart(){
	queryData.beginDate = jQuery('#beginDate').val();
	queryData.endDate = jQuery('#endDate').val();
	if(jQuery('#beginDate').val() == ""){
		alert("查询开始时间不能为空!");
		return;
	}
	if(jQuery('#endDate').val() == ""){
		alert("查询结束时间不能为空!");
		return;
	}
	//状态分析
	var deptUrl = window.rootPath + '/ui/myMoneyInfo/getDeptChart';
	jQuery.ajax({
		url : encodeURI(deptUrl),
		type : 'get',
		data:queryData,
		dataType : 'json',
		contentType : 'application/json;charset=utf-8',
		success : function(data) {
			// 指定图表的配置项和数据
			dept.setOption({
		        series: [{
	              name: '数量',
	              type: 'pie',
	              radius: '55%',
	              data:data.data
		        }]
		    });
		}
	});
	typeChart(null,null,null,3);
}
//查询类型 1：部门 2：片区 3：行业 4：趋势 5：产品类型 6：合同类型
function openContractList(type,value,value1){
	var url = window.rootPath + "/ui/myMoneyTotal";
	if(type == 1){
		var beginDate = value.split("年")[0] + "-" + ((value.split("年")[1].split("月")[0] < 10) ? "0"+value.split("年")[1].split("月")[0] : value.split("年")[1].split("月")[0]) + "-01";
		var endDate = value.split("年")[0] + "-" + (((parseInt(value.split("年")[1].split("月")[0])+1) < 10) ? "0"+(parseInt(value.split("年")[1].split("月")[0])+1) : (parseInt(value.split("年")[1].split("月")[0])+1)) + "-01";
		if(value.split("年")[1].split("月")[0] == 12){//要进一个月
			endDate = parseInt(value.split("年")[0])+1 + "-01-01";
		}
		url += "?beginDate=" +beginDate + "&endDate="+endDate;
	}else if(type == 2){//部门双击
		url += "?deptNameValue=" +value;
	}else if(type == 3){//片区
		url += "?deptNameValue=" +deptNameValue+"&arearNameValue="+value;
	}else if(type == 4){//人员
		url += "?deptNameValue=" +deptNameValue+"&arearNameValue="+arearNameValue+"&personNameValue="+value;
	}else if(type == 5){//类型
		url += "?deptNameValue=" +deptNameValue + "&arearNameValue="+arearNameValue+"&personNameValue="+personNameValue+"&typeNameValue="+value;
	}
	if(type != 1){
		url += "&beginDate=" + jQuery('#beginDate').val() + "&endDate=" + jQuery('#endDate').val();
	}
	
	window.open(url);
}